<template>
<div class="dialog">
    <el-dialog
        :title="dialog.title"
        :visible.sync="dialog.show"
        :close-on-click-model="false"
        :close-on-press-escape="false"
        >
        <div class="form">
            <el-form
                ref="form"
                :model="formData"
                :rules="form_rules"
                label-width="100px"
                style="margin: 10px; width: auto"
                size="small"
            >
                <el-form-item prop="companyName" label="公司名：">
                    <el-input type="companyName" v-model="formData.companyName"></el-input>
                </el-form-item>
                <el-form-item prop="myTime" label="投递时间">
                    <el-date-picker
                        v-model="formData.myTime"
                        type="date"
                        placeholder="选择时间"
                        :picker-options="pickerOptions">
                    </el-date-picker>
                </el-form-item>
                <el-form-item prop="careerName" label="岗位名称：">
                    <el-input type="careerName" v-model="formData.careerName"></el-input>
                </el-form-item>
                <el-form-item prop="link" label="链接相关：">
                    <el-input type="link" v-model="formData.link"></el-input>
                </el-form-item>
                <el-form-item prop="isWriteTest" label="是否已笔试">
                    <el-select v-model="formData.isWriteTest">
                        <el-option v-for="(formtype, index) in format_isWriteTest_list" :key="index" :label="formtype" :value="formtype === '是' ? true : false"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item v-if="!formData.isWriteTest" prop="writeTestTime" label="笔试时间">
                    <el-date-picker
                        v-model="formData.writeTestTime"
                        type="date"
                        placeholder="选择时间"
                        :picker-options="pickerOptions">
                    </el-date-picker>
                </el-form-item>
                <el-form-item prop="isFaceTest" label="是否已面试">
                    <el-select v-model="formData.isFaceTest">
                        <el-option v-for="(formtype, index) in format_isFaceTest_list" :key="index" :label="formtype" :value="formtype === '是' ? true : false"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item v-if="!formData.isFaceTest" prop="faceTestTime" label="面试时间">
                    <el-date-picker
                        v-model="formData.faceTestTime"
                        type="date"
                        placeholder="选择时间"
                        :picker-options="pickerOptions">
                    </el-date-picker>
                </el-form-item>
                <el-form-item prop="source" label="投递渠道：">
                    <el-input type="source" v-model="formData.source"></el-input>
                </el-form-item>
                <el-form-item prop="remark" label="备注：">
                    <el-input type="remark" v-model="formData.remark"></el-input>
                </el-form-item>
                <el-form-item class="text_right">
                    <el-button @click="onCancel()">取消</el-button>
                    <el-button type="primary" @click="onSubmit('form')">提交</el-button>
                </el-form-item>
            </el-form>
        </div>
    </el-dialog>
</div>
</template>

<script>
export default {
    name: 'Dialog',
    props: {
        dialog: Object,
        formData: Object
    },
    data() {
        let formRulesArr = [{ required: true, message: '此字段不能为空！', trigger: 'blur' }]
        return {
            pickerOptions: {
                disabledDate(time) {
                    return time.getTime() > Date.now();
                }
            },
            format_isWriteTest_list: ['是', '否'],
            format_isFaceTest_list: ['是', '否'],
            form_rules: {
                companyName: formRulesArr,
                myTime: formRulesArr,
                isWriteTest: formRulesArr,
                isFaceTest: formRulesArr,
                careerName: formRulesArr,
                source: formRulesArr,
                link: formRulesArr
            }
        }
    },
    methods: {
        onCancel() {
            this.dialog.show = false
            for (let key in this.formData) {
                this.formData[key] = ''
            }
        },
        onSubmit(form) {
            const url = this.dialog.option === 'add' ? 'add' : `edit/${ this.formData.id }`
            this.$refs[form].validate(valid => {
                if (valid) {
                    this.$axios.post('api/profiles/' + url, this.formData)
                    .then(res => {
                        this.$message({
                            message: this.dialog.option === 'add' ? '数据添加成功！' : '数据编辑成功！',
                            type: 'success'
                        })

                        this.dialog.show = false
                        this.$emit('update')
                    })
                }
            })
        }
    },
}
</script>

<style scoped>

</style>